﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using David.Framework.Core.Enums;
using David.WebSite.Data.DataEF;

namespace David.WebSite.Data
{
    /// <summary>
    /// 日志服务
    /// </summary>
    public static class LogDataService
    {
        /// <summary>
        /// 系统错误日志
        /// </summary>
        /// <param name="message">错误</param>
        /// <param name="exceptionMessage">详情</param>
        /// <param name="source">来源</param>
        /// <param name="paras">参数</param>
        public static void Exception(string message, string exceptionMessage, string source, object paras)
        {
            using (var db = new DavidDBEntities())
            {
                var entity = new SystemExceptionLog()
                {
                    Message = message,
                    ExceptionMessage = exceptionMessage,
                    Source = source,
                    OtherParameter = paras.ToString(),
                    CreateTime = DateTime.Now
                };

                db.SystemExceptionLogs.Add(entity);
                db.SaveChanges();
            }
        }
        /// <summary>
        /// 运行日志
        /// </summary>
        /// <param name="message"></param>
        /// <param name="type"></param>
        /// <param name="paras"></param>
        public static void Log(string message, LogType type, string paras, int userId)
        {
            using (var db = new DavidDBEntities())
            {
                var entity = new SystemLog()
                {
                    Message = message,
                    Type = (int)type,
                    OtherParameter = paras,
                    CreateTime = DateTime.Now,
                    UserId = userId
                };
                db.SystemLogs.Add(entity);
                db.SaveChanges();
            }
        }
        /// <summary>
        /// 日志信息
        /// </summary>
        /// <returns></returns>
        public static List<SystemLog> GetSystemLog()
        {
            using (var db = new DavidDBEntities())
            {
                var info = db.SystemLogs.ToList();
                return info;
            }
        }
        /// <summary>
        /// 异常信息
        /// </summary>
        /// <returns></returns>
        public static List<SystemExceptionLog> GetSystemExceptionLog()
        {
            using (var db = new DavidDBEntities())
            {
                var info = db.SystemExceptionLogs.OrderByDescending(c=>c.CreateTime).ToList();
                return info;
            }
        }

    }
}
